package com.meitianhui.convenitenbuy.module.wallet.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletScanningStoreContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.UsableChangeBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletScanningStoreActivity;
import com.meitianhui.convenitenbuy.util.DateUtil;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletScanningStorePresenter implements WalletScanningStoreContract.Presenter {

    private WalletScanningStoreActivity context;
    private final WalletScanningStoreContract.View view;
    private Gson gson;

    public WalletScanningStorePresenter(WalletScanningStoreActivity view,
                                        WalletScanningStoreActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getUsableChange() {
        String user_token = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "finance.usableCashBalanceFind";

        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        map.put("member_type_key", "consumer");
        String jsonParams = new Gson().toJson(map);

        WalletApiDataSource.getInstance()
                .getUsableChange(user_token, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<UsableChangeBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UsableChangeBean> response) {
                        view.getUsableBalanceBack(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        TipUtils.showToast(context, "获取余额失败！");
                    }
                });
    }

    @Override
    public void checkInput(String input) {
        SystemUtils.hidKeyBoard(context);

        if (TextUtils.isEmpty(input)) {
            view.checkInputResult(Constants.WALLET_PAYMENT_SETTING_EMPTY);
            return;
        } else if (!StringUtils.isAmount(input)) {
            view.checkInputResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        } else if ("0".equals(input)
                || "0.".equals(input)
                || "0.0".equals(input)
                || "0.00".equals(input)) {
            view.checkInputResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        }

        BigDecimal number = new BigDecimal(input);
        // 大数据比较
        if (number.compareTo(new BigDecimal("0.00")) == 0) {
            view.checkInputResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        }

        String money = InfoProvider.getUserAsset(App.getInstance()).getCash_balance();
        if (Float.parseFloat(input) > Float.parseFloat(money)) {
            view.checkInputResult(Constants.WALLET_PAYMENT_SETTING_ERROR);
            return;
        }

        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String inputMoney = decimalFormat.format(Double.valueOf(input));
        UserToken userToken = InfoProvider.getUserToken(App.getInstance());
        if (userToken.getSmall_direct_flag().equals("N")
                && userToken.getPayment_password_flag().equals("Y")) {
            view.checkPassword(input);
            return;
        } else if (userToken.getSmall_direct_flag().equals("Y")
                && Float.parseFloat(input) > Constants.SMALL_AMOUNT
                && userToken.getPayment_password_flag().equals("Y")) {
            view.checkPassword(inputMoney);
            return;
        }

        view.checkResultSuccess(inputMoney);
    }

    @Override
    public void payFromStore(String input, String storeId) {
        String date = DateUtil.date2Str(new Date(), "yyyy-MM-dd HH:mm:ss");
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.balancePay";

        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("detail", "扫码支付");
        params.put("amount", input);
        // params.put("out_trade_no", date);
        params.put("payment_way_key", Constants.PAYMENT_WAY_05);
        params.put("buyer_id", token.getMember_id());
        params.put("seller_id", storeId);
        params.put("out_trade_body", gson.toJson(params));
        String jsonParams = gson.toJson(params);

        WalletApiDataSource.getInstance()
                .payForOrder(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.payResult(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.payResult(false, msg);
                    }
                });
    }

}
